package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.messafe.vos.SearchArticleVo;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
@Transactional
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired(required = false)
    private ApArticleMapper apArticleMapper;

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired(required = false)
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    private ArticleFreemarkerService articleFreemarkerService;
    /**
     * 根据参数加载文章列表
     *
     * @param loadtype 1为加载更多  2为加载最新
     * @param dto
     * @return
     */
    @Override
    public ResponseResult load(Short loadtype, ArticleHomeDto dto) {
        //1.参数校验
            //分页size
        if(dto.getSize()==null){
            dto.setSize(10);
        }
            //频道id
        if(dto.getTag()==null){
            dto.setTag("__all__");
        }
            //最小时间，对标是当前页最后一条数据的发布时间
        if(dto.getMinBehotTime()==null){
            dto.setMinBehotTime(new Date());
        }
            //最大时间，对标是当前页第一条数据的发布时间
        if(dto.getMaxBehotTime()==null){
            dto.setMaxBehotTime(new Date());
        }

        //2.调用mapper接口
        List<ApArticle> apArticles = apArticleMapper.loadArticleList(dto, loadtype);

        //3.返回数据
        return ResponseResult.okResult(apArticles);
    }

    /**
     * 保存和修改文章内容+文章配置+文章信息
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult save(ArticleDto dto) throws TemplateException, IOException {
        ApArticle apArticle = new ApArticle();

        //判断保存文章还是修改
        if (dto.getId() == null) {//保存
            //新增ap_article

            BeanUtils.copyProperties(dto, apArticle);
            apArticle.setCreatedTime(new Date());
            save(apArticle);
            //新增ap_article_config
            ApArticleConfig config=new ApArticleConfig(apArticle.getId());
            apArticleConfigMapper.insert(config);

            //新增ap_article_content
            ApArticleContent apArticleContent =new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
        } else {
            //1.先根据id去查询文章，在修改
            apArticle = apArticleMapper.selectById(dto.getId());
            if (apArticle != null) {
                BeanUtils.copyProperties(dto, apArticle);
                apArticleMapper.updateById(apArticle);
                //先查文章内容，在修改
                ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery()
                        .eq(ApArticleContent::getArticleId, apArticle.getId()));

                apArticleContent.setContent(dto.getContent());

                apArticleContentMapper.updateById(apArticleContent);

            }
        }

        //异步调用生成静态文件保存到minio中
        articleFreemarkerService.buildArticleToMinIO(apArticle,dto.getContent());



        return ResponseResult.okResult(apArticle.getId()!=null?apArticle.getId():null);
    }

}
